﻿using StudentLibrary.Module.Util;
using System.Deployment.Application;
using System.Windows.Forms;

namespace StudentLibrary.Module
{
    /// <summary>
    /// Класс книжки
    /// </summary>
    internal class Book
    {
        /// <summary>
        /// Идентификатор книги
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// Жанр
        /// </summary>
        public string Genre { get; set; }
        /// <summary>
        /// Название
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// Инвентарный номер
        /// </summary>
        public string InventoryNum { get; set; }
        /// <summary>
        /// Автор
        /// </summary>
        public string Authors { get; set; }
        /// <summary>
        /// Год выпуска
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// Издатель
        /// </summary>
        public string Publisher { get; set; }
        /// <summary>
        /// Место выпуска
        /// </summary>
        public string Place { get; set; }
        /// <summary>
        /// Количество страниц
        /// </summary>
        public int NumberOfPage { get; set; }

        /// <summary>
        /// Создание книги
        /// </summary>
        /// <returns>При успехе операции вернёт <see langword="true"/>, при неудаче <see langword="false"/></returns>
        internal bool Create()
        {
            string sql = @"INSERT INTO Books (genre, 
                                           title,
                                           inventoryNumber,
                                           authors,
                                           year,
                                           publisher,
                                           place,
                                           numberOfPages)
                            VALUES (@genre, 
                                    @title,
                                    @inventoryNumber,
                                    @authors,
                                    @year,
                                    @publisher,
                                    @place,
                                    @numberOfPages)";
            return sql.SQLNoneQueryWithParametrs(new System.Collections.Generic.Dictionary<string, string>()
            {
                ["@genre"] = Genre,
                ["@title"] = Title,
                ["@inventoryNumber"] = InventoryNum,
                ["@authors"] = Authors,
                ["@year"] = Year.ToString(),
                ["@publisher"] = Publisher,
                ["@place"] = Place,
                ["@numberOfPages"] = NumberOfPage.ToString()
            });
        }

        /// <summary>
        /// Обновление книги
        /// </summary>
        /// <returns>При успехе операции вернёт <see langword="true"/>, при неудаче <see langword="false"/></returns>
        internal bool Update()
        {
            var sql = @"UPDATE Books 
                        SET genre = @genre, 
                            title = @title, 
                            inventoryNumber = @inventoryNumber, 
                            authors = @authors,
                            year = @year, 
                            publisher = @publisher, 
                            place = @place, 
                            numberOfPages = @numberOfPages 
                        WHERE id = @id";
            return sql.SQLNoneQueryWithParametrs(new System.Collections.Generic.Dictionary<string, string>()
            {
                ["@genre"] = Genre,
                ["@title"] = Title,
                ["@inventoryNumber"] = InventoryNum,
                ["@authors"] = Authors,
                ["@year"] = Year.ToString(),
                ["@publisher"] = Publisher,
                ["@place"] = Place,
                ["@numberOfPages"] = NumberOfPage.ToString(),
                ["@id"] = Id.ToString()
            });
        }

        /// <summary>
        /// Удаление книги
        /// </summary>
        /// <returns>При успехе операции вернёт <see langword="true"/>, при неудаче <see langword="false"/></returns>
        internal bool Delete()
        {
            string sql = $@"DELETE 
                            FROM Books 
                            WHERE id = {Id}";
            return sql.SQLNoneQuery();
        }

        /// <summary>
        /// Проверяет на схожесть ID книги в базе данных
        /// </summary>
        /// <param name="id">Идентификатор книги</param>
        /// <returns>При нахождении схожего ID вернёт <see langword="true"/></returns>
        internal static bool IsExist(int id)
        {
            string sql = $@"SELECT COUNT(*) AS ""count""
                            FROM Books
                            WHERE id = {id}";
            return int.Parse(sql.SQLQueryAsDataTable().Rows[0]["count"].ToString()) > 0;
        }
    }
}
